package cn.lwj.service.impl;

import cn.lwj.common.R;
import cn.lwj.common.ServiceException;
import cn.lwj.dto.SetmealDto;
import cn.lwj.mapper.CategoryMapper;
import cn.lwj.mapper.SetmealMapper;
import cn.lwj.pojo.Category;
import cn.lwj.pojo.Setmeal;
import cn.lwj.pojo.SetmealDish;
import cn.lwj.service.CategoryService;
import cn.lwj.service.SetmealDishService;
import cn.lwj.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    SetmealDishService setmealDishService;


    /**
     * 新增套餐和套餐对应的菜品
     * @param setmealDto
     */
    @Transactional
    @Override
    public void saveSetmealWithDish(SetmealDto setmealDto) {
//        1.保存套餐
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        if(setmealDishes.size()>0){
            //        2.保存套餐对应的菜品
            setmealDishService.saveBatch(setmealDishes);
        }
    }

    @Transactional
    @Override
    public void updateSetmealWithDish(SetmealDto setmealDto) {
        //        System.out.println(setmealDto);
//        1.修改套餐本身
        this.updateById(setmealDto);
//        2.修改套餐对应的菜品（通过套餐id删除对应菜品）
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getName,setmealDto.getName()); // 套餐名字相同即可更新（名字唯一）
        Setmeal setmeal = this.getOne(lqw);
        LambdaQueryWrapper<SetmealDish> setmealDishLqw = new LambdaQueryWrapper<>();
        setmealDishLqw.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishService.remove(setmealDishLqw);
//        3.重新添加
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmeal.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void updateStatus(int statusCode, String ids) {
        System.out.println( " statusCode = " + statusCode);
        System.out.println( " ids = " + ids);

        int i = ids.indexOf(",");
        if( i == -1 ){
//            单个修改
            Setmeal setmeal = this.getById(ids);
            setmeal.setStatus(statusCode);
            this.updateById(setmeal);
            return;
        }

//        批量修改
        String[] idsArr = ids.split(",");
//        如果idsArr 长度
        for (String id : idsArr) {
            Setmeal setmeal = this.getById(id);
            setmeal.setStatus(statusCode);
            this.updateById(setmeal);
        }
    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
//        1.判断是否可以删除 启用状态不可以删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);
        long count = this.count(queryWrapper);
        if(count>0){
            throw new ServiceException("套餐为停售状态不可以删除");
        }
//        2.删除套餐
        this.removeByIds(ids);
//        3.删除套餐对应的菜品
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }

    // 删除套餐
    /*@Transactional
    @Override
    public void deleteSetmeal(String ids) {
        System.out.println("ids = " + ids);
        int i = ids.indexOf(",");
        if(i == -1){
//            单个删除
//            1.删除套餐本身
            Setmeal setmeal = this.getById(ids);
            if(setmeal.getStatus() == 0){
                boolean res = this.removeById(ids);
//            2.删除套餐对应的菜品
                LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
//            根据套餐id删除套餐对应的菜品
                lqw.eq(SetmealDish::getSetmealId,ids);
                setmealDishService.remove(lqw);
            }
//            有没有删除都需要退出
            return;
        }
//        批量删除
        String[] idsArr = ids.split(",");
        for (String id : idsArr) {
//            1.删除套餐本身
            this.removeById(id);
//            2.删除套餐对应的菜品
            LambdaQueryWrapper<SetmealDish> lqw = new LambdaQueryWrapper<>();
//            根据套餐id删除套餐菜品
            lqw.eq(SetmealDish::getSetmealId,id);
            setmealDishService.remove(lqw);
        }
    }*/

}
